{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 使用卷积进行范化"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "from torchvision import datasets,transforms\n",
    "\n",
    "data_path = '../data2/p1ch7/'\n",
    "\n",
    "cifar10 = datasets.CIFAR10(data_path,train=True,download=False,transform=transforms.Compose([\n",
    "    transforms.ToTensor(),\n",
    "    transforms.Normalize((0.4912,0.4823,0.4468),(0.2470,0.2435,0.2616))\n",
    "]))\n",
    "cifar10_val = datasets.CIFAR10(data_path,train=False,download=False,transform=transforms.Compose([\n",
    "    transforms.ToTensor(),\n",
    "    transforms.Normalize((0.4912,0.4823,0.4468),(0.2470,0.2435,0.2616))\n",
    "]))\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T05:35:07.189312Z",
     "start_time": "2023-10-16T05:35:05.953291100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).\n"
     ]
    },
    {
     "data": {
      "text/plain": "<Figure size 640x480 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaEAAAGdCAYAAAC7EMwUAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8pXeV/AAAACXBIWXMAAA9hAAAPYQGoP6dpAAApT0lEQVR4nO3df3TU9Z3v8VdkwiSSkIiBzERCTDVUNEhVKEL9AbTkGm9ZkbpFvevC2a5Hy49dLvbYomfXnN5b4nGPHDylsq31UtxKcbcVa48KZC8QdIHewOJCkbVhCRJqxghLfkoCE7/3D4+zjSB+3pCvn2R4Ps6Zc8jM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2uOd//62pfsXc7zvXWmf7tRrrV9S87lz7wxP7bc2zjYMGBxjOhAAA3hBCAABvCCEAgDeEEADAG0IIAOANIQQA8IYQAgB4QwgBALwhhAAA3hBCAABv+u3Yns6INCiE1VlG/EhSJOlem5Vl7J3jXltcbBs8Uj620rk2Z7h1FItttE485r722//bnca1DDPWu/sfD1zmXjvrNVvzAtvoo7bGw861//R/tpl6b97k3vvI/kdNvbuS7gNw/udfzzT1Dpp/71y7+ke/MfW+85szTPW5OYYnljQfw2PFmRAAwBtCCADgDSEEAPCGEAIAeEMIAQC8IYQAAN4QQgAAbwghAIA3hBAAwBtCCADgDSEEAPCm386O65Z7QiYN892sG/wlw5inCWNnmnrHSkc7144pu9rU+6r8651rf/PyL029DyUOmuovaR/iXjz5x6beyv26odh9f3/kmHPlf27dbup8YLetfuXz7znX/sMhU2v1GGr/7+5/NfXe9dIe59pR11tnGLrrKrjCVL+zqcVUP+2rE0z1A9F/nqp3rm071eFcy5kQAMAbUwitXLlS1157rYYOHaqhQ4dq0qRJeu21/5oeHASBqqqqVFRUpOzsbE2ZMkX79u3r80UDANKDKYRGjhypxx9/XDt37tTOnTs1bdo03XHHHamgeeKJJ7Rs2TKtWLFCdXV1isVimj59utrb20NZPABgYDOF0IwZM3T77bdr9OjRGj16tH7wgx8oJydHO3bsUBAEWr58uR599FHNmjVL5eXlWr16tT744AOtWbMmrPUDAAawc35NqKenR2vXrlVnZ6cmTZqkhoYGJRIJVVRUpGqi0ahuvfVWbdv26R+y1d3drba2tl4XAMCFwRxCe/fuVU5OjqLRqB588EGtW7dOV199tRKJhCSpsLD3J0YWFhambjuT6upq5eXlpS7FxcXWJQEABihzCH3xi1/Um2++qR07dujb3/625syZo7feeit1e0ZGRq/6IAhOu+6PLVmyRK2tralLY2OjdUkAgAHK/D6hwYMH68orr5QkjR8/XnV1dXrqqaf03e9+V5KUSCQUj8dT9c3NzaedHf2xaDSqaDRqXQYAIA2c9/uEgiBQd3e3SktLFYvFVFNTk7rt5M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duyYGhsbB9SxDYJACxYs0IsvvqhNmzaptLS01+3pcjw/azvPZCAezzMJgkDd3d2f/7Hs8z91CMHatWuDzMzM4Nlnnw3eeuutYNGiRcGQIUOCQ4cO+V5an3nooYeCLVu2BAcPHgx27NgRfP3rXw9yc3MH9Da2t7cHu3fvDnbv3h1ICpYtWxbs3r07eOedd4IgCILHH388yMvLC1588cVg7969wT333BPE4/Ggra3N88ptzrad7e3twUMPPRRs27YtaGhoCDZv3hxMmjQpuOyyywbUdn77298O8vLygi1btgRNTU2pywcffJCqSYfj+VnbmS7Hc8mSJcHWrVuDhoaGYM+ePcEjjzwSXHTRRcHGjRuDIPh8j+WACKEgCIIf/ehHQUlJSTB48ODg+uuv7/Unk+lg9uzZQTweDzIzM4OioqJg1qxZwb59+3wv67xs3rw5kHTaZc6cOUEQfPRnvY899lgQi8WCaDQa3HLLLcHevXv9LvocnG07P/jgg6CioiIYPnx4kJmZGYwaNSqYM2dOcPjwYd/LNjnT9kkKVq1alapJh+P5WduZLsfzL/7iL1LPp8OHDw+++tWvpgIoCD7fY8lHOQAAvOn3rwkBANIXIQQA8IYQAgB4QwgBALwhhAAA3hBCAABvCCEAgDeEEADAG0IIAOANIQQA8IYQAgB4QwgBALz5/02PANfE+/bJAAAAAElFTkSuQmCC"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "label_map = {0:1,2:1}\n",
    "class_names = ['airplane','bird']\n",
    "cifar2 = [(img,label_map[label]) for img,label in cifar10 if label in [0,2]]\n",
    "cifar2_val = [(img,label_map[label])for img,label in cifar10_val if label in [0,2]]\n",
    "\n",
    "img,label = cifar2[66]\n",
    "print(label)\n",
    "import matplotlib.pyplot as plt\n",
    "plt.imshow(img.permute(1,2,0))\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T05:53:39.671817500Z",
     "start_time": "2023-10-16T05:53:21.142895Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 8.2.卷积实战"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "Conv2d(3, 16, kernel_size=(3, 3), stride=(1, 1))"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch.nn as nn\n",
    "conv = nn.Conv2d(3,16,kernel_size=3)\n",
    "conv"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T06:46:45.963317700Z",
     "start_time": "2023-10-16T06:46:45.910277400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "(torch.Size([16, 3, 3, 3]), torch.Size([16]))"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conv.weight.shape,conv.bias.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T06:49:08.163637500Z",
     "start_time": "2023-10-16T06:49:08.143642700Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 调用卷积函数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "(torch.Size([1, 3, 32, 32]), torch.Size([1, 16, 30, 30]))"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img,_ = cifar2[0]\n",
    "output = conv(img.unsqueeze(0))\n",
    "img.unsqueeze(0).shape,output.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T06:53:16.731538800Z",
     "start_time": "2023-10-16T06:53:16.245094900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "<Figure size 640x480 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSU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msYNk+/btWrp0qbKyspSfn6+nnnpKp06dGpAZjfs6ke0ejfvbMSJK6O2339bmzZv10ksv6ejRo3rkkUdUUVGh8+fPh17akJo/f75qamr6T8ePHw+9pEHX2tqqRYsWaefOnbf8/quvvqodO3Zo586dOnz4sAoLC7V69Wo1Nzff45UOnrttsyQ9+eSTA/b9Bx98cA9XOPj279+vjRs36tChQ9qzZ4+6u7tVXl4+YGjvaNzXiWy3NPr2tyUaAX7/938/+s53vjPgvLlz50Z/9Vd/FWhFQ+/ll1+OFi1aFHoZ95Sk6Gc/+1n/1729vVFhYWH0gx/8oP+8jo6OKCcnJ/qnf/qnACscfF/e5iiKovXr10ff+MY3gqznXqmvr48kRfv374+iaGzs6yi6ebujaGzs7zsZ9kdCXV1dOnLkiMrLywecX15eroMHDwZa1b1RWVmp4uJilZWV6dlnn9WZM2dCL+meqqqqUm1t7YB9H4/H9dhjj436fb9v3z7l5+drzpw5eu6551RfXx96SYOqsbFRkpSbmytp7OzrL293n9G+v+9k2JfQlStX1NPTo4KCggHnFxQUqLa2NtCqht6yZcv05ptvavfu3frxj3+s2tparVixQg0NDaGXds/07d+xtu8rKir0k5/8RHv37tUPf/hDHT58WE888YT92TrDVRRF2rJli1auXKkFCxZIGhv7+lbbLY3+/X03w26K9u18+WMdoii66bzRpKKiov/fCxcu1MMPP6z7779fb7zxhrZs2RJwZffeWNv369at6//3ggULtGTJEpWWlur999/X2rVrA65scGzatEnHjh3Tr371q5u+N5r39e22e7Tv77sZ9kdCeXl5SklJuel/Q/X19Tf9r2k0y8jI0MKFC1VZWRl6KfdM36sBx/q+LyoqUmlp6ajY9y+88ILee+89ffjhhwM+smW07+vbbfetjKb9nYhhX0JpaWlavHix9uzZM+D8PXv2aMWKFYFWde91dnbq008/VVFRUeil3DNlZWUqLCwcsO+7urq0f//+MbXvGxoaVF1dPaL3fRRF2rRpk9555x3t3btXZWVlA74/Wvf13bb7VkbD/rYEfFFEwn76059Gqamp0b/8y79EJ0+ejDZv3hxlZGREZ8+eDb20IfO9730v2rdvX3TmzJno0KFD0R/90R9FWVlZo26bm5ubo6NHj0ZHjx6NJEU7duyIjh49Gp07dy6Koij6wQ9+EOXk5ETvvPNOdPz48eib3/xmVFRUFDU1NQVeefLutM3Nzc3R9773vejgwYNRVVVV9OGHH0YPP/xwNG3atBG9zd/97nejnJycaN++fVFNTU3/qa2trT8zGvf13bZ7tO5vx4gooSiKon/8x3+MSktLo7S0tOj3fu/3BrzEcTRat25dVFRUFKWmpkbFxcXR2rVroxMnToRe1qD78MMPI0k3ndavXx9F0ecv3X355ZejwsLCKB6PR48++mh0/PjxsIv+iu60zW1tbVF5eXk0derUKDU1NZoxY0a0fv366Pz586GX/ZXcanslRbt27erPjMZ9fbftHq3728FHOQAAghn2zwkBAEYvSggAEAwlBAAIhhICAARDCQEAgqGEAADBUEIAgGAoIQBAMJQQACAYSggAEAwlBAAIhhICAATz/0nig6j2QCzVAAAAAElFTkSuQmCC"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(output[0,0].detach(),cmap='gray')\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T06:54:28.998500100Z",
     "start_time": "2023-10-16T06:54:28.640382500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "(torch.Size([1, 3, 32, 32]), torch.Size([1, 1, 32, 32]))"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 指定填充\n",
    "conv = nn.Conv2d(3,1,kernel_size=3,padding=1)\n",
    "output = conv(img.unsqueeze(0))\n",
    "img.unsqueeze(0).shape,output.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T06:59:06.352082300Z",
     "start_time": "2023-10-16T06:59:06.343088Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 8.2.2.用卷积检测特征"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [],
   "source": [
    "import torch\n",
    "with torch.no_grad():\n",
    "    conv.bias.zero_()\n",
    "\n",
    "with torch.no_grad():\n",
    "    conv.weight.fill_(1.0/9.0)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T07:04:06.049026600Z",
     "start_time": "2023-10-16T07:04:05.905675700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "<Figure size 640x480 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaEAAAGdCAYAAAC7EMwUAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8pXeV/AAAACXBIWXMAAA9hAAAPYQGoP6dpAAAohUlEQVR4nO3dbWxUZ37+8Wvww/iRAQf8tDiuuwvbbiBIDSmBZhNCixVXRcmyldiNtAK1jTYLRELsKi3Ji1iViqNUQaxEQ9ttRIk2LHnRJI2UbBJXBNMVpYIoURDZpkRxGqfgOBj8bI+xff4vVsy/Dg85F3i47eH7kUaKZ37cvs+5z8wvxzPnmkQURZEAAAhgVugJAABuXjQhAEAwNCEAQDA0IQBAMDQhAEAwNCEAQDA0IQBAMDQhAEAw+aEn8GUTExM6ffq0ysvLlUgkQk8HAGCKokj9/f2qra3VrFlXP9eZdk3o9OnTqqurCz0NAMB16ujo0IIFC65ak7Um9Oyzz+pv//ZvdebMGd12223atWuXvv3tb3/lvysvL5ckPfbYY0omk7F+Vzqdjj2vgYGB2LWSVFRUFLv24tzjKikpiV2bSqWssefOnZuVeUjShQsXrPrBwcHYtcPDw9bY08VX/d/elxUUFGRt/MLCQmtsp96d99jYWOza/v5+a2znuHJeIyRpfHzcqneeE868JWlkZCQrtW59Xl5e7NrR0VE999xzsV4Ts9KE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3xMCAOQumhAAIBiaEAAgGJoQACAYmhAAIBiaEAAgGJoQACAYmhAAIBiaEAAgGJoQACAYmhAAIBiaEAAgmP8Hqbq0HeBzZqUAAAAASUVORK5CYII="
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "output = conv(img.unsqueeze(0))\n",
    "plt.imshow(output[0,0].detach(),cmap='gray')\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T07:06:15.491753Z",
     "start_time": "2023-10-16T07:06:15.267746100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 设计卷积核"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [],
   "source": [
    "conv = nn.Conv2d(3,1,kernel_size=3,padding=1)\n",
    "with torch.no_grad():\n",
    "    conv.weight[:] = torch.tensor([[-1.0,0.0,1.0],\n",
    "                                   [-1.0,0.0,1.0],\n",
    "                                   [-1.0,0.0,1.0]\n",
    "                                   ])\n",
    "# 事实上,该卷积核的原理是用右边的元素减去左边的元素。相当于一个边缘检测核:卷积核突出显示2个水平相邻区域的垂直边缘"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T07:14:24.617074300Z",
     "start_time": "2023-10-16T07:14:24.532179400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "<Figure size 640x480 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaEAAAGdCAYAAAC7EMwUAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8pXeV/AAAACXBIWXMAAA9hAAAPYQGoP6dpAAApx0lEQVR4nO3dfWzV9d3/8dcB2tO700NLb05ra9MheKlVrkwcwpwiG41dRnRsCZvJAtlm5gATgovXhV6Jz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Pev115O+SuhgoICzZw584IJ3NraesGkTiXZ2dm68847dfz4cd9LSYpzr/y70fZVkkpKSlRRUTEt9/aJJ57Qm2++qffff3/CR66k2n5e6jgvZrruZ3p6um655RYtXLhQdXV1WrBggX7xi19c972c8kMoPT1dd999t+rr6yd8vb6+XkuWLPG0quSLx+P6+OOPVVJS4nspSVFZWalYLDZhX4eHh9XQ0JDS+ypJ7e3tampqmlZ7GwSB1q9fr9dee0179uxRZWXlhO+nyn5e6TgvZjru58UEQaB4PH7993LSX+qQBLt37w7S0tKC3/zmN8FHH30UbNiwIcjOzg5Onjzpe2mT5sknnwz27t0bnDhxIjhw4EDwjW98I4hEItP6GHt7e4MPPvgg+OCDDwJJwZYtW4IPPvgg+Oyzz4IgCILnnnsuiEajwWuvvRYcPXo0+O53vxuUlJQEPT09nlduc7nj7O3tDZ588slg//79QWNjY/D+++8HixcvDm666aZpdZw//vGPg2g0Guzduzdobm5O3AYGBhI1qbCfVzrOVNnPTZs2Bfv27QsaGxuDDz/8MHj66aeDGTNmBO+9914QBNd3L6fFEAqCIPjv//7voKKiIkhPTw+++MUvTnjJZCpYtWpVUFJSEqSlpQWlpaXBypUrg2PHjvle1jV5//33A0kX3FavXh0EwT9f1vvss88GsVgsCIfDwf333x8cPXrU76KvwuWOc2BgIKiurg4KCwuDtLS04Oabbw5Wr14dnDp1yveyTS52fJKCHTt2JGpSYT+vdJypsp/f//73E8+nhYWFwVe/+tXEAAqC67uXfJQDAMCbKf87IQBA6mIIAQC8YQgBALxhCAEAvGEIAQC8YQgBALxhCAEAvGEIAQC8YQgBALxhCAEAvGEIAQC8YQgBALz5P80VoB8d+bnzAAAAAElFTkSuQmCC"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "output = conv(img.unsqueeze(0))\n",
    "plt.imshow(output[0,0].detach(),cmap='gray')\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T07:16:49.697025900Z",
     "start_time": "2023-10-16T07:16:49.483026200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 8.2.3.使用深度和池化技术进一步研究"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "(torch.Size([1, 3, 32, 32]), torch.Size([1, 3, 16, 16]))"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 池化操作\n",
    "pool = nn.MaxPool2d(2)\n",
    "output = pool(img.unsqueeze(0))\n",
    "\n",
    "img.unsqueeze(0).shape,output.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T07:24:17.424713100Z",
     "start_time": "2023-10-16T07:24:17.173176300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [],
   "source": [
    "model = nn.Sequential(\n",
    "    nn.Conv2d(3,16,kernel_size=3,padding=1),\n",
    "    nn.Tanh(),\n",
    "    nn.MaxPool2d(2),\n",
    "    nn.Conv2d(16,8,kernel_size=3,padding=1),\n",
    "    nn.Tanh(),\n",
    "    nn.MaxPool2d(2),\n",
    "    nn.Flatten(),# 将数据变化为(512,)的形式\n",
    "    nn.Linear(8*8*8,32),\n",
    "    nn.Tanh(),\n",
    "    nn.Linear(32,2),\n",
    "    nn.Softmax(dim=1)\n",
    ")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T07:41:42.257990800Z",
     "start_time": "2023-10-16T07:41:42.151999200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "data": {
      "text/plain": "(18090, [432, 16, 1152, 8, 16384, 32, 64, 2])"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numel_list = [p.numel() for p in model.parameters()]\n",
    "sum(numel_list),numel_list"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T07:41:43.384367200Z",
     "start_time": "2023-10-16T07:41:43.364392600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[0.4069, 0.5931]], grad_fn=<SoftmaxBackward0>)"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model(img.unsqueeze(0))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T07:52:32.521053500Z",
     "start_time": "2023-10-16T07:52:32.489051800Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 8.3.子类化nn.Module\n",
    "<p>为了子类化Module(定义自己的网络模型)，我们至少需要定义一个forward方法,该方法接收模块的输入并返回输出。</p>\n",
    "<P>通常情况下,运算将使用其他模块,例如,内置的卷积或自定义模块(全连接层)。为了包含这些模块,我们通常在构造函数__init__()中定义他们,并将他们付给self.以便在forward中使用self访问这些模块.同时,这些模块将会在模型的整个生命周期中保存自己的参数</P>"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [],
   "source": [
    "class Net(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.conv1 = nn.Conv2d(3,16,kernel_size=3,padding=1)\n",
    "        self.act1 = nn.Tanh()\n",
    "        self.pool1 = nn.MaxPool2d(2)\n",
    "        self.conv2 = nn.Conv2d(16,8,kernel_size=3,padding=1)\n",
    "        self.act2 = nn.Tanh()\n",
    "        self.pool2 = nn.MaxPool2d(2)\n",
    "        self.fc1 = nn.Linear(8*8*8,32)\n",
    "        self.act3 = nn.Tanh()\n",
    "        self.fc2 = nn.Linear(32,2)\n",
    "\n",
    "    def forward(self,x):\n",
    "        out = self.pool1(self.act1(self.conv1(x)))\n",
    "        out = self.pool2(self.act2(self.conv2(out)))\n",
    "        out = out.view(-1,8*8*8)\n",
    "        out = self.act3(self.fc1(out))\n",
    "        out = self.fc2(out)\n",
    "        return out"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T08:13:30.408843200Z",
     "start_time": "2023-10-16T08:13:30.377864Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "data": {
      "text/plain": "(18090, [432, 16, 1152, 8, 16384, 32, 64, 2])"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = Net()\n",
    "numel_list = [p.numel() for p in model.parameters()]\n",
    "sum(numel_list), numel_list"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T12:41:55.684732700Z",
     "start_time": "2023-10-16T12:41:55.642707800Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 8.3.3.函数式API\n",
    "<p>这里的函数式指的是”没有内部状态“</p>"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [],
   "source": [
    "import torch.nn.functional as F\n",
    "class Net(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.conv1 = nn.Conv2d(3,16,kernel_size=3,padding=1)\n",
    "        self.conv2 = nn.Conv2d(16,8,kernel_size=3,padding=1)\n",
    "        self.fc1 = nn.Linear(8*8*8,32)\n",
    "        self.fc2 = nn.Linear(32,2)\n",
    "\n",
    "    def forward(self,x):\n",
    "        out = F.max_pool2d(F.tanh(self.conv1(x)),2)\n",
    "        out = F.max_pool2d(F.tanh(self.conv2(out)),2)\n",
    "        out = out.view(-1,8*8*8)\n",
    "        out = F.tanh(self.fc1(out))\n",
    "        out = self.fc2(out)\n",
    "        return out"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T12:56:23.239439800Z",
     "start_time": "2023-10-16T12:56:23.225443400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[-0.0870, -0.0838]], grad_fn=<AddmmBackward0>)"
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = Net()\n",
    "model(img.unsqueeze(0))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T12:56:24.562539400Z",
     "start_time": "2023-10-16T12:56:24.418541400Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 训练"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "outputs": [],
   "source": [
    "import datetime\n",
    "def train_loop(n_epochs,optimizer,model,loss_fn,train_loader):\n",
    "    for epoch in range(1,n_epochs+1):\n",
    "        loss_train = 0.0\n",
    "        for imgs,labels in train_loader:\n",
    "            imgs = imgs.cuda()\n",
    "            labels = labels.cuda()\n",
    "            outputs = model(imgs) # 前向传播\n",
    "            loss = loss_fn(outputs,labels) # 计算损失值\n",
    "            optimizer.zero_grad() # 梯度归0\n",
    "            loss.backward() # 计算梯度\n",
    "            optimizer.step() # 更新梯度\n",
    "            loss_train+=loss.item()# 将item转化为数字\n",
    "\n",
    "        if epoch==1 or epoch%10==0:\n",
    "            print('{} Epoch{},Training loss {}'.format(datetime.datetime.now(),epoch,loss_train/len(train_loader)))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T13:16:42.146038600Z",
     "start_time": "2023-10-16T13:16:42.135045800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-10-16 21:16:43.769072 Epoch1,Training loss 0.10925405869367207\n",
      "2023-10-16 21:16:49.717073 Epoch10,Training loss 0.0011841172291630299\n",
      "2023-10-16 21:16:55.620521 Epoch20,Training loss 0.0005561572593743586\n",
      "2023-10-16 21:17:01.560520 Epoch30,Training loss 0.00036180010573363085\n",
      "2023-10-16 21:17:07.393845 Epoch40,Training loss 0.0002676194548906438\n",
      "2023-10-16 21:17:13.144834 Epoch50,Training loss 0.00021214144399677327\n",
      "2023-10-16 21:17:19.034002 Epoch60,Training loss 0.00017561302646389528\n",
      "2023-10-16 21:17:24.925003 Epoch70,Training loss 0.00014975966685851975\n",
      "2023-10-16 21:17:31.091005 Epoch80,Training loss 0.0001305055680905712\n",
      "2023-10-16 21:17:37.336004 Epoch90,Training loss 0.00011561610946353157\n",
      "2023-10-16 21:17:43.172382 Epoch100,Training loss 0.00010376017937209801\n"
     ]
    }
   ],
   "source": [
    "train_loader = torch.utils.data.DataLoader(cifar2,batch_size=64,shuffle=False)\n",
    "model = Net().cuda() # 初始化我们的网络\n",
    "from torch import optim\n",
    "optimizer = optim.SGD(model.parameters(),lr=0.01) # 我们一直在用的随机梯度下降器\n",
    "loss_fn = nn.CrossEntropyLoss() # 交叉熵损失\n",
    "train_loop(\n",
    "    n_epochs=100,\n",
    "    optimizer=optimizer,\n",
    "    model=model,\n",
    "    loss_fn=loss_fn,\n",
    "    train_loader=train_loader\n",
    ")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T13:17:43.179363300Z",
     "start_time": "2023-10-16T13:16:42.866041200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "精度train 1.000\n",
      "精度val 1.000\n"
     ]
    }
   ],
   "source": [
    "## 测量准确率\n",
    "train_loader = torch.utils.data.DataLoader(cifar2,batch_size=64,shuffle=False)\n",
    "val_loader= torch.utils.data.DataLoader(cifar2_val,batch_size=64,shuffle=False)\n",
    "\n",
    "def validate(model,train_loader,val_loader):\n",
    "    for name,loader in [('train',train_loader),('val',val_loader)]:\n",
    "        correct = 0\n",
    "        total = 0\n",
    "        with torch.no_grad():\n",
    "            for imgs,labels in loader:\n",
    "                imgs = imgs.cuda()\n",
    "                labels = labels.cuda()\n",
    "                outputs = model(imgs)\n",
    "                _,pred = torch.max(outputs,dim=1)\n",
    "                total+=labels.shape[0]\n",
    "                correct += int((pred==labels).sum())\n",
    "        print('精度{} {:.3f}'.format(name,correct/total))\n",
    "\n",
    "\n",
    "validate(model,train_loader,val_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-16T13:36:22.652181200Z",
     "start_time": "2023-10-16T13:36:22.114175700Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 8.4.3.在GPU上训练"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### 准备数据集"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "from torchvision import datasets,transforms\n",
    "data_path ='../data2/p1ch7/'\n",
    "cifar10 = datasets.CIFAR10(data_path,train=True,download=False,transform=transforms.Compose(\n",
    "    [transforms.ToTensor(),\n",
    "    transforms.Normalize((0.4915,0.4823,0.4468),(0.2470,0.2435,0.2616))\n",
    "]))\n",
    "\n",
    "cifar10_val = datasets.CIFAR10(data_path,train=True,download=False,transform=transforms.Compose(\n",
    "    [\n",
    "        transforms.ToTensor(),\n",
    "        transforms.Normalize((0.4915,0.4823,0.4468),(0.2470,0.2435,0.2616))\n",
    "    ]\n",
    "))\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T05:26:29.526771500Z",
     "start_time": "2023-10-17T05:25:58.829810800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bird\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).\n"
     ]
    },
    {
     "data": {
      "text/plain": "<Figure size 640x480 with 1 Axes>",
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xPqxp6hUJBb7/9tiorK30vZUjU1tYqnU4POK69vb1qbm4u6uMqSceOHVNra+uIOrZhGGr58uV67rnntHXrVtXW1g64vViO56dt59mMxON5NmEYqlAoXPxjOehvdRgCmzdvDseMGRP+5Cc/Cd96661wxYoV4fjx48NDhw75Xtqg+d73vhdu27YtfO+998KdO3eGX/va18JkMjmit7Grqyvcs2dPuGfPnlBSuGbNmnDPnj3h73//+zAMw/Cxxx4LS0tLw+eeey7cv39/eNddd4WVlZVhZ2en55XbnGs7u7q6wu9973vhjh07wpaWlvCNN94I58yZE372s58dUdt53333haWlpeG2bdvCtra2/suJEyf6a4rheH7adhbL8Vy1alW4ffv2sKWlJdy3b1/48MMPh6NGjQpff/31MAwv7rEcEUMoDMPwr//6r8Oamppw7Nix4Re/+MUBb5ksBosXLw4rKyvDMWPGhFVVVeGiRYvCAwcO+F7WBXnjjTdCSWdclixZEobhR2/rffTRR8N0Oh3GYrHw5ptvDvfv3+930efhXNt54sSJsK6uLiwvLw/HjBkTXnbZZeGSJUvC999/3/eyTc62fZLCDRs29NcUw/H8tO0sluP57W9/u//5tLy8PPyTP/mT/gEUhhf3WPJVDgAAb4b9a0IAgOLFEAIAeMMQAgB4wxACAHjDEAIAeMMQAgB4wxACAHjDEAIAeMMQAgB4wxACAHjDEAIAeMMQAgB48/8Bz2gClLh18/UAAAAASUVORK5CYII="
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "label_map = {0:0,2:1}\n",
    "class_names = ['airplane','bird']\n",
    "cifar2 = [(img,label_map[label])\n",
    "    for img,label in cifar10\n",
    "          if label in [0,2]\n",
    "]\n",
    "cifar2_val = [(img,label_map[label])\n",
    "              for img,label in cifar10_val\n",
    "                if label in [0,2]\n",
    "              ]\n",
    "img,label = cifar2_val[8]\n",
    "print(class_names[label])\n",
    "import matplotlib.pyplot as plt\n",
    "plt.imshow(img.permute(1,2,0))\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T05:33:04.116531400Z",
     "start_time": "2023-10-17T05:32:35.067538300Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 搭建模型"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [],
   "source": [
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "class Net(nn.Module):\n",
    "    def __init__(self, *args, **kwargs):\n",
    "        super().__init__()\n",
    "        #super().__init__(*args, **kwargs)\n",
    "        self.c1 = nn.Conv2d(3,16,3,padding=1)\n",
    "        self.c2 = nn.Conv2d(16,8,3,padding=1)\n",
    "        self.fc1 = nn.Linear(8*8*8,32)\n",
    "        self.fc2 = nn.Linear(32,2)\n",
    "\n",
    "    def forward(self,x):\n",
    "        out = F.max_pool2d(F.tanh(self.c1(x)),2)\n",
    "        out = F.max_pool2d(F.tanh(self.c2(out)),2)\n",
    "        out=out.view(-1,8*8*8)\n",
    "        out = F.tanh(self.fc1(out))\n",
    "        out = self.fc2(out)\n",
    "\n",
    "        return out"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T05:46:44.240829500Z",
     "start_time": "2023-10-17T05:46:44.227823200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[-0.0625,  0.0369]], grad_fn=<AddmmBackward0>)"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = Net()\n",
    "model(img.unsqueeze(0))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T05:46:45.250826100Z",
     "start_time": "2023-10-17T05:46:45.139825400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training on device cuda.\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "device = (torch.device('cuda') if torch.cuda.is_available()\n",
    "          else torch.device('cpu'))\n",
    "print(f'Training on device {device}.')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T05:52:57.116357400Z",
     "start_time": "2023-10-17T05:52:57.105360700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [],
   "source": [
    "train_loader = torch.utils.data.DataLoader(cifar2,shuffle=False,batch_size=64)\n",
    "test_loader = torch.utils.data.DataLoader(cifar2_val,shuffle=False,batch_size=64)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T06:10:23.204920200Z",
     "start_time": "2023-10-17T06:10:23.185925400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [],
   "source": [
    "import datetime\n",
    "def training_loop(n_epochs,optimizer,model,loss_fn,train_loader):\n",
    "    for epoch in range(1,n_epochs+1):\n",
    "        loss_train =0.0\n",
    "        for imgs,labels in train_loader:\n",
    "            imgs = imgs.to(device)\n",
    "            labels = labels.to(device)\n",
    "            output = model(imgs)\n",
    "            loss = loss_fn(output,labels)\n",
    "            optimizer.zero_grad()\n",
    "            loss.backward()\n",
    "            optimizer.step()\n",
    "\n",
    "            loss_train+= loss.item()\n",
    "\n",
    "        if epoch == 1 or epoch % 10==0:\n",
    "            print('{} Epoch {}, Traing loss {}'.format(datetime.datetime.now(),epoch,loss_train/len(train_loader)))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T06:13:36.483852Z",
     "start_time": "2023-10-17T06:13:36.471822300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-10-17 17:28:38.453987 Epoch 1, Traing loss 0.5507349986938914\n",
      "2023-10-17 17:28:44.465989 Epoch 10, Traing loss 0.3331481310402512\n",
      "2023-10-17 17:28:51.222145 Epoch 20, Traing loss 0.28769511534909536\n",
      "2023-10-17 17:28:57.957815 Epoch 30, Traing loss 0.2624960518946314\n",
      "2023-10-17 17:29:04.535058 Epoch 40, Traing loss 0.2437501570126813\n",
      "2023-10-17 17:29:11.213057 Epoch 50, Traing loss 0.2276438564346854\n",
      "2023-10-17 17:29:17.816443 Epoch 60, Traing loss 0.21230359740887478\n",
      "2023-10-17 17:29:24.348621 Epoch 70, Traing loss 0.1970483249729606\n",
      "2023-10-17 17:29:30.998920 Epoch 80, Traing loss 0.18171877048577473\n",
      "2023-10-17 17:29:37.677266 Epoch 90, Traing loss 0.16658188312486477\n",
      "2023-10-17 17:29:44.560665 Epoch 100, Traing loss 0.15180997526759554\n"
     ]
    }
   ],
   "source": [
    "import torch.optim as optim\n",
    "model = Net().to(device=device)\n",
    "optimizer = optim.SGD(model.parameters(),lr=0.01)\n",
    "loss_fn = nn.CrossEntropyLoss()\n",
    "training_loop(\n",
    "    n_epochs=100,\n",
    "    model=model,\n",
    "    optimizer=optimizer,\n",
    "    loss_fn=loss_fn,\n",
    "    train_loader=train_loader\n",
    ")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T09:29:44.860116200Z",
     "start_time": "2023-10-17T09:28:37.556987600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度:0.934\n"
     ]
    }
   ],
   "source": [
    "def test_model(model,test_loader):\n",
    "    total = 0\n",
    "    correct = 0\n",
    "    with torch.no_grad():\n",
    "        for imgs,labels in test_loader:\n",
    "            imgs = imgs.to(device)\n",
    "            labels = labels.to(device)\n",
    "            outputs = model(imgs)\n",
    "            _,pred = torch.max(outputs,dim=1)\n",
    "            total+=labels.shape[0]\n",
    "            correct += ((pred==labels).sum())\n",
    "    print('测试集精度:{:.3f}'.format(correct/total))\n",
    "\n",
    "test_model(model,test_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T09:30:32.347662400Z",
     "start_time": "2023-10-17T09:30:31.310638200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 8.4.2.保存并加载我们的模型"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [],
   "source": [
    "torch.save(model.state_dict(),data_path+'birds_vs_airpline.pt')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T06:29:20.189980Z",
     "start_time": "2023-10-17T06:29:18.901659300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "data": {
      "text/plain": "<All keys matched successfully>"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 加载模型\n",
    "loaded_model = Net()\n",
    "loaded_model.load_state_dict(torch.load(data_path+'birds_vs_airpline.pt'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T06:32:23.137432200Z",
     "start_time": "2023-10-17T06:32:21.321393Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[-1.6450,  1.7137]], grad_fn=<AddmmBackward0>)"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loaded_model(img.unsqueeze(0))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T06:33:14.753409300Z",
     "start_time": "2023-10-17T06:33:13.644264300Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 8.5.模型设计（针对比较复杂的图像）\n",
    "\n",
    "### 8.5.1.增加内存容量:宽度（增加第一个卷积层的通道数）"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "outputs": [],
   "source": [
    "class NetWidth(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.conv1 = nn.Conv2d(3,32,kernel_size=3,padding=1)\n",
    "        self.conv2 = nn.Conv2d(32,16,kernel_size=3,padding=1)\n",
    "        self.fc1 = nn.Linear(16*8*8,32)\n",
    "        self.fc2 = nn.Linear(32,2)\n",
    "\n",
    "    def forward(self,x):\n",
    "        out = F.max_pool2d(F.tanh(self.conv1(x)),2)\n",
    "        out = F.max_pool2d(F.tanh(self.conv2(out)),2)\n",
    "        out = out.view(-1,16*8*8)\n",
    "        out = F.tanh(self.fc1(out))\n",
    "        out = self.fc2(out)\n",
    "        return out"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T09:16:10.882896800Z",
     "start_time": "2023-10-17T09:16:10.860891100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[864, 32, 4608, 16, 32768, 32, 64, 2]\n",
      "38386\n"
     ]
    }
   ],
   "source": [
    "model = NetWidth()\n",
    "p_nums=[p.numel() for p in model.parameters()]\n",
    "print(p_nums)\n",
    "print(sum(p_nums))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T09:16:12.817397500Z",
     "start_time": "2023-10-17T09:16:12.796358100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 8.5.2 帮助我们的模型收敛和范化:正则化\n",
    "<P>训练模型设计两个关键步骤,一是优化(减小模型的损失值),而是范化</P>"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 检查参数：权重惩罚\n",
    "<P>稳定范化的第一种方法是给损失中添加一个正则化项,以便减小模型的权重，从而限制训练对他们增长的影响。其中比较流行的正则化项是l2正则化,他是模型中所有权重的平方和。因此，在损失函数中加入l2正则化,相当于在优化步骤中将每个权重按当前值的比例递减(因此也被称为权重衰减)</P>"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "outputs": [],
   "source": [
    "def training_loop_l2ref(n_epochs,optimizer,model,loss_fn,train_loader):\n",
    "    for epoch in range(1,n_epochs+1):\n",
    "        loss_train = 0.0\n",
    "        for imgs,labels in train_loader:\n",
    "            imgs,labels = imgs.to(device),labels.to(device)\n",
    "            outputs = model(imgs)\n",
    "            loss = loss_fn(outputs,labels)\n",
    "            l2_lambda = 0.001\n",
    "            l2_norm = sum(p.pow(2.0).sum() for p in model.parameters())\n",
    "            loss = loss+l2_lambda*l2_norm # 为损失值添加l2正则化项目\n",
    "\n",
    "            optimizer.zero_grad()\n",
    "            loss.backward()\n",
    "            optimizer.step()\n",
    "\n",
    "            loss_train+=loss.item()\n",
    "        if epoch == 1 or epoch%10==0:\n",
    "            print('{} Epoch {},Training loss {}'.format(\n",
    "                datetime.datetime.now(),epoch,loss_train/len(train_loader)\n",
    "            ))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T09:16:15.659493400Z",
     "start_time": "2023-10-17T09:16:15.639502Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-10-17 17:31:33.775398 Epoch 1,Training loss 0.5762220167430343\n",
      "2023-10-17 17:31:43.139118 Epoch 10,Training loss 0.34433169928705615\n",
      "2023-10-17 17:31:53.724434 Epoch 20,Training loss 0.3049444025678999\n",
      "2023-10-17 17:32:04.287650 Epoch 30,Training loss 0.2802440518406546\n",
      "2023-10-17 17:32:14.658438 Epoch 40,Training loss 0.2590625474976886\n",
      "2023-10-17 17:32:25.068897 Epoch 50,Training loss 0.23947358549020853\n",
      "2023-10-17 17:32:35.431935 Epoch 60,Training loss 0.22181468423764417\n",
      "2023-10-17 17:32:46.109495 Epoch 70,Training loss 0.20564019385796445\n",
      "2023-10-17 17:32:56.999093 Epoch 80,Training loss 0.19047386996495497\n",
      "2023-10-17 17:33:08.194304 Epoch 90,Training loss 0.17648464436553846\n",
      "2023-10-17 17:33:18.576304 Epoch 100,Training loss 0.16352060010099106\n"
     ]
    }
   ],
   "source": [
    "model = NetWidth()\n",
    "optimizer = optim.SGD(model.parameters(),lr=0.01)\n",
    "training_loop_l2ref(n_epochs=100,model=model.to(device),optimizer=optimizer,loss_fn=loss_fn,train_loader=train_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T09:33:18.582336300Z",
     "start_time": "2023-10-17T09:31:32.542410400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "outputs": [],
   "source": [
    "def test_loop_l2reg(model,test_loader):\n",
    "    total = 0\n",
    "    correct = 0\n",
    "    with torch.no_grad():\n",
    "        for imgs,labels in test_loader:\n",
    "            imgs,labels = imgs.to(device),labels.to(device)\n",
    "            output = model(imgs)\n",
    "            _,pred = torch.max(output,dim=1)\n",
    "            total+=labels.shape[0]\n",
    "            correct += int((pred==labels).sum())\n",
    "    print(\"测试集精度:{}\".format(correct/total))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T11:21:39.870949600Z",
     "start_time": "2023-10-17T11:21:39.780960100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度:0.9435\n"
     ]
    }
   ],
   "source": [
    "test_loop_l2reg(model.to(device),test_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T11:21:42.772440700Z",
     "start_time": "2023-10-17T11:21:41.567438100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.不太依赖单一输入:Dropout\n",
    "<p>思想为：将网络每轮训练迭代中的神经元部分清零</p>"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "outputs": [],
   "source": [
    "class NetDropout(nn.Module):\n",
    "    def __init__(self,n_chans1=32):\n",
    "        super().__init__()\n",
    "        self.n_chans1 = n_chans1\n",
    "        self.conv1 = nn.Conv2d(3,n_chans1,kernel_size=3,padding=1)\n",
    "        self.conv1_dropout = nn.Dropout2d(p=0.4) # 在第一个卷积层添加dropout\n",
    "        self.conv2 = nn.Conv2d(n_chans1,n_chans1//2,kernel_size=3,padding=1)\n",
    "        self.conv2_dropout = nn.Dropout2d(p=0.4) # 在第二个卷积层添加dropout\n",
    "        self.fc1 = nn.Linear(8*8*n_chans1//2,32)\n",
    "        self.fc2 = nn.Linear(32,2)\n",
    "\n",
    "    def forward(self,x):\n",
    "        out = F.max_pool2d(torch.tanh(self.conv1(x)),2)\n",
    "        out = self.conv1_dropout(out)\n",
    "        out = F.max_pool2d(torch.tanh(self.conv2(out)),2)\n",
    "        out = self.conv2_dropout(out)\n",
    "        out = out.view(-1,8*8*self.n_chans1//2)\n",
    "        out = torch.tanh(self.fc1(out))\n",
    "        out = self.fc2(out)\n",
    "        return out"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T11:45:41.393392300Z",
     "start_time": "2023-10-17T11:45:41.370357900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "outputs": [
    {
     "data": {
      "text/plain": "<Figure size 640x480 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaEAAAGdCAYAAAC7EMwUAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8pXeV/AAAACXBIWXMAAA9hAAAPYQGoP6dpAAAvAElEQVR4nO3dbXCc5Xn//d/uane1klZrC1lPtnFMsAnB4P+/mBg7BAwt+qNOGYjTGRJmMmbaMiE8zHicDK3hBZrO1GbojYfM7eK2aYbCFAovCpS5IYA7xnZT17ltBopvQ4iJZSzHkoVlSavHfTzvF8SaCBs4DyNxS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/6q0ArmnwPPfSQW758eehlTBlJ7oUXXhj/ulwuu6amJvfwww+PXzc2NuYymYz7+7//+wArnByf3E7nnFu3bp275ZZbgqxnqvT09DhJbteuXc652bs/P7mdzs3O/emcc3PnznX/9E//9KXvy2l/JpTP5/Xmm2+qtbV1wvWtra3as2dPoFVNjUOHDqmlpUWLFy/Wd7/7XR0+fDj0kqZMR0eHuru7J+zXZDKp6667btbtV0nauXOnGhoatHTpUt15553q6ekJvaQvZGBgQJJUV1cnafbuz09u52mzaX+WSiU9++yzGh4e1qpVq770fTnth9DJkydVKpXU2Ng44frGxkZ1d3cHWtXkW7lypZ566im99tpr+ulPf6ru7m6tXr1avb29oZc2JU7vu9m+XyWpra1NTz/9tHbs2KFHH31U+/bt0w033KBcLhd6aefEOacNGzbommuu0bJlyyTNzv15tu2UZs/+PHDggGpqapRMJnXXXXfphRde0Ne//vUvfV9OuxTtT/P7H+sgfXyAfPK6maytrW38/y+//HKtWrVKX/3qV/Xkk09qw4YNAVc2tWb7fpWk2267bfz/ly1bphUrVmjRokV6+eWXtXbt2oArOzf33nuv3nnnHf3iF78447bZtD8/bTtny/685JJL9Pbbb6u/v1//9m//pnXr1mnXrl3jt39Z+3LanwnV19crFoudMYF7enrOmNSzSXV1tS6//HIdOnQo9FKmxOlX/p1v+1WSmpubtWjRohm5b++77z699NJLeuONNyZ85Mps25+ftp1nM1P3ZyKR0MUXX6wVK1Zo8+bNWr58uX7yk5986fty2g+hRCKhK6+8Utu3b59w/fbt27V69epAq5p6uVxO7733npqbm0MvZUosXrxYTU1NE/ZrPp/Xrl27ZvV+laTe3l51dnbOqH3rnNO9996r559/Xjt27NDixYsn3D5b9ufnbefZzMT9eTbOOeVyuS9/X076Sx2mwLPPPuvi8bj72c9+5t599123fv16V11d7Y4cORJ6aZPmRz/6kdu5c6c7fPiw27t3r/uTP/kTl06nZ/Q2Dg4Ourfeesu99dZbTpLbsmWLe+utt9yHH37onHPu4YcfdplMxj3//PPuwIED7nvf+55rbm522Ww28MptPms7BwcH3Y9+9CO3Z88e19HR4d544w23atUqN3/+/Bm1nT/84Q9dJpNxO3fudF1dXeOXkZGR8ZrZsD8/bztny/7cuHGj2717t+vo6HDvvPOOe+CBB1w0GnWvv/66c+7L3ZczYgg559zf/d3fuUWLFrlEIuH+4A/+YMJLJmeD2267zTU3N7t4PO5aWlrc2rVr3cGDB0Mv6wt54403nKQzLuvWrXPOffyy3oceesg1NTW5ZDLprr32WnfgwIGwiz4Hn7WdIyMjrrW11c2bN8/F43F34YUXunXr1rmjR4+GXrbJ2bZPknviiSfGa2bD/vy87Zwt+/PP/uzPxp9P582b5/7wD/9wfAA59+XuSz7KAQAQzLT/mxAAYPZiCAEAgmEIAQCCYQgBAIJhCAEAgmEIAQCCYQgBAIJhCAEAgmEIAQCCYQgBAIJhCAEAgmEIAQCC+f8BNn1PXzvZFxAAAAAASUVORK5CYII="
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "cifar10 = datasets.CIFAR10(data_path,train=False,download=False)\n",
    "img,label = cifar10[58]\n",
    "plt.imshow(img)\n",
    "plt.show()\n",
    "print(label)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T11:29:19.555698500Z",
     "start_time": "2023-10-17T11:29:11.537020500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "outputs": [],
   "source": [
    "def train_loop_dropout(n_epochs,model,optimizer,loss_fn,train_loader):\n",
    "    for epoch in range(1,n_epochs+1):\n",
    "        loss_train =0.0\n",
    "        for imgs,labels in train_loader:\n",
    "            imgs,labels = imgs.to(device),labels.to(device)\n",
    "            outputs = model(imgs)\n",
    "            loss = loss_fn(outputs,labels)\n",
    "            optimizer.zero_grad()\n",
    "            loss.backward()\n",
    "            optimizer.step()\n",
    "            loss_train+=loss.item()\n",
    "        if epoch == 1 or epoch %10 ==0:\n",
    "            print('{} Epoch:{} 训练损失:{}'.format(datetime.datetime.now(),epoch,loss_train/len(train_loader)))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T11:46:46.223193200Z",
     "start_time": "2023-10-17T11:46:46.217174900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-10-17 19:46:48.167334 Epoch:1 训练损失:0.5753062760374349\n",
      "2023-10-17 19:46:55.435342 Epoch:10 训练损失:0.3789618756550892\n",
      "2023-10-17 19:47:03.546336 Epoch:20 训练损失:0.35021826995026534\n",
      "2023-10-17 19:47:11.867372 Epoch:30 训练损失:0.33006223409798496\n",
      "2023-10-17 19:47:19.973384 Epoch:40 训练损失:0.31584222216135377\n",
      "2023-10-17 19:47:28.142371 Epoch:50 训练损失:0.2987387671022658\n",
      "2023-10-17 19:47:36.205370 Epoch:60 训练损失:0.28471599965338495\n",
      "2023-10-17 19:47:44.503336 Epoch:70 训练损失:0.274879413235719\n",
      "2023-10-17 19:47:52.634388 Epoch:80 训练损失:0.2633939626490235\n",
      "2023-10-17 19:48:00.752338 Epoch:90 训练损失:0.2504325408464784\n",
      "2023-10-17 19:48:08.889335 Epoch:100 训练损失:0.24501556585169143\n"
     ]
    }
   ],
   "source": [
    "model = NetDropout(n_chans1=32).to(device)\n",
    "optimizer = optim.SGD(model.parameters(),lr=0.01)\n",
    "train_loader = torch.utils.data.DataLoader(cifar2,shuffle=False,batch_size=64)\n",
    "train_loop_dropout(n_epochs=100,model=model,loss_fn=loss_fn,optimizer=optimizer,train_loader=train_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T11:48:08.896390100Z",
     "start_time": "2023-10-17T11:46:46.805914900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度:0.926\n"
     ]
    }
   ],
   "source": [
    "def test_dropout(model,test_loader):\n",
    "    correct =0\n",
    "    total = 0\n",
    "    with torch.no_grad():\n",
    "        for imgs,labels in test_loader:\n",
    "            imgs,labels = imgs.to(device),labels.to(device)\n",
    "            outputs = model(imgs)\n",
    "            _,preds = torch.max(outputs,dim=1)\n",
    "            correct+= int((preds==labels).sum())\n",
    "            total+=labels.shape[0]\n",
    "    print(\"测试集精度:{:.3f}\".format(correct/total))\n",
    "\n",
    "test_dropout(model.eval(),test_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T11:53:55.190056500Z",
     "start_time": "2023-10-17T11:53:54.232029400Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3.保持激活检查:批量归一化\n",
    "<p>批量归一化的原理:要求输入数据服从标准正态分布,从而使这个小批量具有一定的理想分布。</p>"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "outputs": [],
   "source": [
    "class NetBatchNorm(nn.Module):\n",
    "    def __init__(self,n_chans1=32):\n",
    "        super().__init__()\n",
    "        self.n_chans1 = n_chans1\n",
    "        self.conv1 = nn.Conv2d(3,n_chans1,kernel_size=3,padding=1)\n",
    "        self.conv1_batchnorm = nn.BatchNorm2d(num_features=n_chans1)\n",
    "        self.conv2 = nn.Conv2d(n_chans1,n_chans1//2,kernel_size=3,padding=1)\n",
    "        self.conv2_batchnorm = nn.BatchNorm2d(num_features=n_chans1//2)\n",
    "        self.fc1 = nn.Linear(8*8*n_chans1//2,32)\n",
    "        self.fc2 = nn.Linear(32,2)\n",
    "\n",
    "    def forward(self,x):\n",
    "        out = self.conv1_batchnorm(self.conv1(x)) # 先做批量归一化,之后才池化\n",
    "        out = F.max_pool2d(torch.tanh(out),2)\n",
    "        out = self.conv2_batchnorm(self.conv2(out))\n",
    "        out = F.max_pool2d(torch.tanh(out),2)\n",
    "        out = out.view(-1,8*8*self.n_chans1//2)\n",
    "        out = torch.tanh(self.fc1(out))\n",
    "        out = self.fc2(out)\n",
    "        return out"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T12:25:20.024704100Z",
     "start_time": "2023-10-17T12:25:20.018691700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-10-17 20:25:40.984854 Epoch 1, Traing loss 0.4714686006877073\n",
      "2023-10-17 20:25:50.235259 Epoch 10, Traing loss 0.27123263729795527\n",
      "2023-10-17 20:25:59.880246 Epoch 20, Traing loss 0.21126463197788614\n",
      "2023-10-17 20:26:09.369280 Epoch 30, Traing loss 0.16243913375837787\n",
      "2023-10-17 20:26:18.858272 Epoch 40, Traing loss 0.12124897290472013\n",
      "2023-10-17 20:26:28.361321 Epoch 50, Traing loss 0.08612135273351032\n",
      "2023-10-17 20:26:37.885012 Epoch 60, Traing loss 0.058391529906564835\n",
      "2023-10-17 20:26:47.425609 Epoch 70, Traing loss 0.03984519316678404\n",
      "2023-10-17 20:26:56.997146 Epoch 80, Traing loss 0.027162336240148847\n",
      "2023-10-17 20:27:06.544088 Epoch 90, Traing loss 0.01764844227908476\n",
      "2023-10-17 20:27:16.019086 Epoch 100, Traing loss 0.012165639854991321\n"
     ]
    }
   ],
   "source": [
    "model = NetBatchNorm().to(device)\n",
    "optimizer = optim.SGD(model.parameters(),lr=0.01)\n",
    "training_loop(n_epochs=100,model=model,optimizer=optimizer,loss_fn=loss_fn,train_loader=train_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T12:27:16.027097Z",
     "start_time": "2023-10-17T12:25:31.768714200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度:0.989\n"
     ]
    }
   ],
   "source": [
    "test_dropout(model.eval(),test_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-17T12:29:34.387385600Z",
     "start_time": "2023-10-17T12:29:33.127439800Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 8.5.3.升入学习更复杂的结构:深度"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [],
   "source": [
    "from torchvision import datasets,transforms\n",
    "data_path = '../data2/p1ch7/'\n",
    "cifar10 = datasets.CIFAR10(data_path,train=True,download=False,transform=transforms.Compose([\n",
    "    transforms.ToTensor(),\n",
    "    transforms.Normalize((0.4912,0.4823,0.4468),(0.2470,0.2435,0.2616))\n",
    "]))\n",
    "cifar10_val = datasets.CIFAR10(data_path,train=False,download=False,transform=transforms.Compose([\n",
    "    transforms.ToTensor(),\n",
    "    transforms.Normalize((0.4912,0.4823,0.4468),(0.2470,0.2435,0.2616))\n",
    "]))\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T02:13:35.525122700Z",
     "start_time": "2023-10-18T02:13:30.362625500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [],
   "source": [
    "label_map = {0:1,2:1}\n",
    "class_names = ['air_plane','bird']\n",
    "cifar2 = [(img,label_map[label]) for img,label in cifar10 if label in [0,2]]\n",
    "cifar2_val = [(img,label_map[label]) for img,label in cifar10_val if label in [0,2]]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T02:13:51.537007700Z",
     "start_time": "2023-10-18T02:13:37.047008900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).\n"
     ]
    },
    {
     "data": {
      "text/plain": "<Figure size 640x480 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaE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qdPfdd+utt94aVFMK61nIdpbCeq5du1Y33nhj/gOpM2bM0Msvv5y//UKu5bAIoY0bN2rJkiV65JFHtGvXLt12221qbGzUwYMHY09tSF1//fXq6urKX9rb22NP6bz09/dr6tSpWrNmzRlvf/zxx7Vq1SqtWbNGbW1tqqqq0ty5c9XX13eBZ3p+Pms7JenOO+8ctLabNm26gDM8f62trVq0aJF27NihlpYWnTx5Ug0NDerv78/XlMJ6FrKd0vBfzwkTJuixxx7Tzp07tXPnTs2ZM0d33XVXPmgu6FqGYeDLX/5yuP/++wdd98d//Mfhe9/7XqQZDb1HH300TJ06NfY0EiMpPP/88/mfT506FaqqqsJjjz2Wv+748eMhk8mEv//7v48ww6Hxye0MIYSmpqZw1113RZlPUnp6eoKk0NraGkIo3fX85HaGUJrrGUIIn//858M//uM/XvC1LPozoRMnTuj1119XQ0PDoOsbGhq0ffv2SLNKxr59+1RTU6P6+nrdc8892r9/f+wpJaajo0Pd3d2D1jWdTmvWrFklt66StHXrVlVUVGjy5Mm677771NPTE3tK56W3t1eSNG7cOEmlu56f3M6PlNJ6DgwMaMOGDerv79eMGTMu+FoWfQi99957GhgYUGVl5aDrKysr1d3dHWlWQ2/69Ol65pln9Oqrr+qHP/yhuru7NXPmTB0+fDj21BLx0dqV+rpKUmNjo5599llt2bJFTzzxhNra2jRnzhzlcrnYUzsnIQQtXbpUt956q6ZMmSKpNNfzTNsplc56tre3a+zYsUqn07r//vv1/PPP67rrrrvga1l0XbQ/zce/1kH68AD55HXDWWNjY/7fN9xwg2bMmKGrr75aTz/9tJYuXRpxZskq9XWVpAULFuT/PWXKFE2bNk11dXV66aWXNH/+/IgzOzeLFy/W7t279Ytf/OK020ppPT9tO0tlPa+99lq98cYbOnLkiH72s5+pqalJra2t+dsv1FoW/ZnQ+PHjNWLEiNMSuKen57SkLiVjxozRDTfcoH379sWeSiI+euffxbauklRdXa26urphubYPPvigXnzxRb322muDvnKl1Nbz07bzTIbreo4aNUrXXHONpk2bpubmZk2dOlVPPvnkBV/Log+hUaNG6aabblJLS8ug61taWjRz5sxIs0peLpfTm2++qerq6thTSUR9fb2qqqoGreuJEyfU2tpa0usqSYcPH1ZnZ+ewWtsQghYvXqznnntOW7ZsUX19/aDbS2U9P2s7z2Q4rueZhBCUy+Uu/FoO+VsdErBhw4YwcuTI8KMf/Sjs3bs3LFmyJIwZMyYcOHAg9tSGzEMPPRS2bt0a9u/fH3bs2BH+7M/+LJSVlQ3rbezr6wu7du0Ku3btCpLCqlWrwq5du8I777wTQgjhscceC5lMJjz33HOhvb09fP3rXw/V1dUhm81GnrnnbNvZ19cXHnroobB9+/bQ0dERXnvttTBjxoxw1VVXDavt/M53vhMymUzYunVr6Orqyl8++OCDfE0prOdnbWeprOfy5cvDtm3bQkdHR9i9e3d4+OGHwyWXXBI2b94cQriwazksQiiEEP7u7/4u1NXVhVGjRoUvfelLg94yWQoWLFgQqqurw8iRI0NNTU2YP39+2LNnT+xpnZfXXnstSDrt0tTUFEL48G29jz76aKiqqgrpdDrcfvvtob29Pe6kz8HZtvODDz4IDQ0N4corrwwjR44MEydODE1NTeHgwYOxp2050/ZJCuvWrcvXlMJ6ftZ2lsp6fvOb38w/nl555ZXhT//0T/MBFMKFXUu+ygEAEE3RvyYEAChdhBAAIBpCCAAQDSEEAIiGEAIAREMIAQCiIYQAANEQQgCAaAghAEA0hBAAIBpCCAAQDSEEAIjm/wOCwVA0R0roHgAAAABJRU5ErkJggg=="
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "'bird'"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "img,label = cifar2[9]\n",
    "plt.imshow(img.permute(1,2,0))\n",
    "plt.show()\n",
    "class_names[label]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T01:29:13.262021Z",
     "start_time": "2023-10-18T01:29:08.599282200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### 1.跳跃连接"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "import torch.nn as nn\n",
    "import torch\n",
    "import torch.nn.functional as F\n",
    "class NetDepth(nn.Module):\n",
    "    def __init__(self,n_chans1=32):\n",
    "        super().__init__()\n",
    "        self.n_chans1 = n_chans1\n",
    "        self.conv1 = nn.Conv2d(3,n_chans1,kernel_size=3,padding=1)\n",
    "        self.conv2 = nn.Conv2d(n_chans1,n_chans1//2,kernel_size=3,padding=1)\n",
    "        self.conv3 = nn.Conv2d(n_chans1//2,n_chans1//2,kernel_size=3,padding=1)\n",
    "        self.fc1 = nn.Linear(4*4*n_chans1//2,32)\n",
    "        self.fc2 = nn.Linear(32,2)\n",
    "\n",
    "    def forward(self,x):\n",
    "        out = F.max_pool2d(torch.relu(self.conv1(x)),2)\n",
    "        out = F.max_pool2d(torch.relu(self.conv2(out)),2)\n",
    "        out = F.max_pool2d(torch.relu(self.conv3(out)),2)\n",
    "        out = out.view(-1,4*4*self.n_chans1//2)\n",
    "        out = torch.relu(self.fc1(out))\n",
    "        out = self.fc2(out)\n",
    "        return out"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T01:40:47.842969Z",
     "start_time": "2023-10-18T01:40:47.777968600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [],
   "source": [
    "import datetime\n",
    "def train(n_epochs,model,optimizer,loss_fn,train_loader):\n",
    "    for epoch in range(1,n_epochs+1):\n",
    "        loss_train = 0.0\n",
    "        for imgs,labels in train_loader:\n",
    "            imgs,labels=imgs.cuda(),labels.cuda()\n",
    "            outputs = model(imgs)\n",
    "            loss = loss_fn(outputs,labels)\n",
    "\n",
    "            # 反向传播\n",
    "            optimizer.zero_grad()\n",
    "            loss.backward()\n",
    "            optimizer.step()\n",
    "\n",
    "            loss_train+=loss # 计算损失\n",
    "        if epoch == 1 or epoch %10 ==0:\n",
    "            print(\"{}, Epoch:{}, 训练损失:{}\".format(datetime.datetime.now(),epoch,loss_train/len(train_loader)))\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T01:56:24.539790100Z",
     "start_time": "2023-10-18T01:56:24.530801700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-10-18 09:56:29.883319, Epoch:1, 训练损失:0.12292050570249557\n",
      "2023-10-18 09:56:36.952298, Epoch:10, 训练损失:0.000130985674331896\n",
      "2023-10-18 09:56:44.460298, Epoch:20, 训练损失:5.5860829888843e-05\n",
      "2023-10-18 09:56:52.043985, Epoch:30, 训练损失:3.460803782218136e-05\n",
      "2023-10-18 09:56:59.606996, Epoch:40, 训练损失:2.478695569152478e-05\n",
      "2023-10-18 09:57:07.158013, Epoch:50, 训练损失:1.9183791664545424e-05\n",
      "2023-10-18 09:57:14.849478, Epoch:60, 训练损失:1.5582350897602737e-05\n",
      "2023-10-18 09:57:22.508476, Epoch:70, 训练损失:1.3081619727017824e-05\n",
      "2023-10-18 09:57:30.072196, Epoch:80, 训练损失:1.1248775081185158e-05\n",
      "2023-10-18 09:57:37.645229, Epoch:90, 训练损失:9.85067254077876e-06\n",
      "2023-10-18 09:57:45.354531, Epoch:100, 训练损失:8.750753295316827e-06\n"
     ]
    }
   ],
   "source": [
    "import torch.optim as optim\n",
    "train_loader = torch.utils.data.DataLoader(cifar2,shuffle=False,batch_size=64)\n",
    "loss_fn = nn.CrossEntropyLoss()\n",
    "model = NetDepth().cuda()\n",
    "optimizer = optim.SGD(model.parameters(),lr=0.01)\n",
    "train(n_epochs=100,model=model,loss_fn=loss_fn,optimizer=optimizer,train_loader=train_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T01:57:45.366520300Z",
     "start_time": "2023-10-18T01:56:25.524312400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [],
   "source": [
    "def test_loop(model,test_loader):\n",
    "    correct = 0\n",
    "    total = 0\n",
    "    with torch.no_grad():\n",
    "        for imgs,labels in test_loader:\n",
    "            imgs,labels = imgs.cuda(),labels.cuda()\n",
    "            outputs = model(imgs)\n",
    "            _,pred = torch.max(outputs,dim=1)\n",
    "            total+=labels.shape[0]\n",
    "            correct +=int((pred==labels).sum())\n",
    "    print('测试集精度:{:.3f}'.format(correct/total))\n",
    "\n",
    "test_loader = torch.utils.data.DataLoader(cifar2_val,shuffle=False,batch_size=64)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T02:14:12.017849700Z",
     "start_time": "2023-10-18T02:14:11.999847Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度:1.000\n"
     ]
    }
   ],
   "source": [
    "test_loop(model,test_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T02:14:13.071762900Z",
     "start_time": "2023-10-18T02:14:12.792762600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [],
   "source": [
    "class NetRes(nn.Module):\n",
    "    def __init__(self,n_chans1=32):\n",
    "        super().__init__()\n",
    "        self.n_chans1=n_chans1\n",
    "        self.conv1 = nn.Conv2d(3,n_chans1,kernel_size=3,padding=1)\n",
    "        self.conv2 = nn.Conv2d(n_chans1,n_chans1//2,kernel_size=3,padding=1)\n",
    "        self.conv3 = nn.Conv2d(n_chans1//2,n_chans1//2,kernel_size=3,padding=1)\n",
    "        self.fc1 = nn.Linear(4*4*n_chans1//2,32)\n",
    "        self.fc2 = nn.Linear(32,2)\n",
    "\n",
    "    def forward(self,x):\n",
    "        out = F.max_pool2d(torch.relu(self.conv1(x)),2)\n",
    "        out = F.max_pool2d(torch.relu(self.conv2(out)),2)\n",
    "        out1 = out\n",
    "        out = F.max_pool2d(torch.relu(self.conv3(out))+out1,2) # 添加一个跳跃连接,相当于将第一层的输出添加到第三层的输入中\n",
    "        out = out.view(-1,4*4*self.n_chans1//2)\n",
    "        out = torch.relu(self.fc1(out))\n",
    "        out = self.fc2(out)\n",
    "        return out"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T02:28:29.299096200Z",
     "start_time": "2023-10-18T02:28:29.235085400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-10-18 10:30:59.873358, Epoch:1, 训练损失:0.05352352559566498\n",
      "2023-10-18 10:31:06.676358, Epoch:10, 训练损失:0.000120948301628232\n",
      "2023-10-18 10:31:14.101417, Epoch:20, 训练损失:5.2528586820699275e-05\n",
      "2023-10-18 10:31:21.536543, Epoch:30, 训练损失:3.2755109714344144e-05\n",
      "2023-10-18 10:31:28.993544, Epoch:40, 训练损失:2.3541377231595106e-05\n",
      "2023-10-18 10:31:36.442543, Epoch:50, 训练损失:1.826070001698099e-05\n",
      "2023-10-18 10:31:43.899543, Epoch:60, 训练损失:1.4856576854072046e-05\n",
      "2023-10-18 10:31:51.354515, Epoch:70, 训练损失:1.2488115316955373e-05\n",
      "2023-10-18 10:31:58.799733, Epoch:80, 训练损失:1.0748468412202783e-05\n",
      "2023-10-18 10:32:06.294735, Epoch:90, 训练损失:9.420716196473222e-06\n",
      "2023-10-18 10:32:13.877732, Epoch:100, 训练损失:8.373977834708057e-06\n"
     ]
    }
   ],
   "source": [
    "model = NetRes()\n",
    "optimizer = optim.SGD(model.parameters(),lr=0.01)\n",
    "train(n_epochs=100,model=model.cuda(),optimizer=optimizer,loss_fn=loss_fn,train_loader=train_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T02:32:13.886736200Z",
     "start_time": "2023-10-18T02:30:58.090370900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度:1.000\n"
     ]
    }
   ],
   "source": [
    "test_loop(model,test_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T02:32:58.347402Z",
     "start_time": "2023-10-18T02:32:58.078400200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 使用Pytorch构建非常深的模型\n",
    "<p>我们可以使用残差块构建比较深层次的模型。残差块是卷积层,激活函数层,批量归一化层的组合,即多层组成一个残差块,而多个残差块最终组合成一个深度学习模型</p>"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [],
   "source": [
    "## 首先构建一个残差块\n",
    "class ResBlock(nn.Module):\n",
    "    def __init__(self,n_chans):\n",
    "        super(ResBlock,self).__init__()\n",
    "        self.conv = nn.Conv2d(n_chans,n_chans,kernel_size=3,padding=1,bias=False)\n",
    "        self.batch_norm = nn.BatchNorm2d(num_features=n_chans)\n",
    "        # 使用自定义的初始化\n",
    "        torch.nn.init.kaiming_normal_(self.conv.weight,nonlinearity='relu')\n",
    "        torch.nn.init.constant_(self.batch_norm,0.5)\n",
    "        torch.nn.init.zeros_(self.batch_norm.bias)\n",
    "\n",
    "    def forward(self,x):\n",
    "        out = self.conv(x)\n",
    "        out = self.batch_norm(out)\n",
    "        out = torch.relu(out)\n",
    "        return out+x"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T02:57:05.200220700Z",
     "start_time": "2023-10-18T02:57:05.125240800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [],
   "source": [
    "class NetResDeep(nn.Module):\n",
    "    def __init__(self,n_chans1=32,n_blocks=10):\n",
    "        super().__init__()\n",
    "        self.n_chans1 = n_chans1\n",
    "        self.conv1 = nn.Conv2d(3,n_chans1,kernel_size=3,padding=1)\n",
    "        self.resblocks = nn.Sequential(\n",
    "            *(n_blocks*[ResBlock(n_chans=n_chans1)])\n",
    "        )\n",
    "        self.fc1 = nn.Linear(8*8*n_chans1,32)\n",
    "        self.fc2 = nn.Linear(32,2)\n",
    "\n",
    "    def forward(self,x):\n",
    "        out = F.max_pool2d(torch.relu(self.conv1(x)),2)\n",
    "        out = self.resblocks(out)\n",
    "        out = F.max_pool2d(out,2)\n",
    "        out = out.view(-1,8*8*self.n_chans1)\n",
    "        out = torch.relu(self.fc1(out))\n",
    "        out = self.fc2(out)\n",
    "        return out"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T03:11:32.040689400Z",
     "start_time": "2023-10-18T03:11:32.031688400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-10-18 11:11:43.674949, Epoch:1, 训练损失:0.20952937006950378\n",
      "2023-10-18 11:11:50.359921, Epoch:10, 训练损失:0.0005603801109828055\n",
      "2023-10-18 11:11:57.833932, Epoch:20, 训练损失:0.00023036419588606805\n",
      "2023-10-18 11:12:05.269988, Epoch:30, 训练损失:0.00014030902821104974\n",
      "2023-10-18 11:12:12.718000, Epoch:40, 训练损失:9.942182077793404e-05\n",
      "2023-10-18 11:12:20.191997, Epoch:50, 训练损失:7.636453665327281e-05\n",
      "2023-10-18 11:12:27.622987, Epoch:60, 训练损失:6.166846287669614e-05\n",
      "2023-10-18 11:12:35.126098, Epoch:70, 训练损失:5.153251549927518e-05\n",
      "2023-10-18 11:12:42.670086, Epoch:80, 训练损失:4.414336945046671e-05\n",
      "2023-10-18 11:12:50.134106, Epoch:90, 训练损失:3.852967347484082e-05\n",
      "2023-10-18 11:12:57.597106, Epoch:100, 训练损失:3.413079321035184e-05\n"
     ]
    }
   ],
   "source": [
    "model = NetRes().cuda()\n",
    "optimizer = optim.SGD(model.parameters(),lr=3e-3)\n",
    "train(n_epochs=100,model=model,loss_fn=loss_fn,optimizer=optimizer,train_loader=train_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T03:12:57.606136100Z",
     "start_time": "2023-10-18T03:11:41.419890600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度:1.000\n"
     ]
    }
   ],
   "source": [
    "test_loop(model,test_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T03:13:29.424463400Z",
     "start_time": "2023-10-18T03:13:29.074491700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
